package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.LoginActivity;
import com.zzk.lanouhn.myapplication1.activity.MainActivity;
import com.zzk.lanouhn.myapplication1.activity.MineArticlesActivity;
import com.zzk.lanouhn.myapplication1.activity.MineCollectActivity;
import com.zzk.lanouhn.myapplication1.activity.MineCompanyActivity;
import com.zzk.lanouhn.myapplication1.view.MineFragmentTouXiangPop;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 16/8/23.
 */
public class MineFragment extends Fragment {
    private CircleImageView civ;
    private RelativeLayout ll_shou, ll_wenzhang, ll_dongtai;
    private RelativeLayout rel_company,rel_coupon,rel_understand,rel_exit;
    private TextView tv_denglu, tv_tuichu;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String shifou;

    private MineFragmentTouXiangPop touXiangPop;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, null);
        civ = (CircleImageView) view.findViewById(R.id.mine_iv_touxiang);
        ll_shou = (RelativeLayout) view.findViewById(R.id.mine_shouchang);
        ll_wenzhang = (RelativeLayout) view.findViewById(R.id.mine_wenzhang);
        ll_dongtai = (RelativeLayout) view.findViewById(R.id.mine_dongtai);

        rel_company= (RelativeLayout) view.findViewById(R.id.mine_company);
        rel_coupon= (RelativeLayout) view.findViewById(R.id.mine_coupon);
        rel_understand= (RelativeLayout) view.findViewById(R.id.mine_understand);
        rel_exit= (RelativeLayout) view.findViewById(R.id.rel_exit);
        //登录
        tv_denglu = (TextView) view.findViewById(R.id.mine_iv_denglu);
        tv_tuichu = (TextView) view.findViewById(R.id.mine_tv_tuichu);

        sp = getActivity().getSharedPreferences("loginpic", Context.MODE_PRIVATE);
        shifou = sp.getString("shifou", "false");
        editor = sp.edit();

        initDeta();
        return view;
    }

    private void initDeta() {
        if (shifou.equals("false")) {
            rel_exit.setVisibility(View.GONE);

            tv_denglu.setVisibility(View.VISIBLE);
            //登录的点击事件
            tv_denglu.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    getActivity().startActivity(intent);
                    getActivity().finish();
                }
            });
        } else {
            rel_exit.setVisibility(View.VISIBLE);

            tv_denglu.setVisibility(View.GONE);
        }


        //civ的点击事件
        civ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                touXiangPop = new MineFragmentTouXiangPop(getActivity(), itemsOnClick);
                touXiangPop.showAsDropDown(ll_dongtai);
            }
        });
        //退出
        tv_tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareSDK.initSDK(getActivity());
                Platform qq = ShareSDK.getPlatform(getActivity(), QQ.NAME);
                if (qq.isValid()) {
                    qq.removeAccount();
                }
                editor.putString("shifou", "false");
                editor.commit();
                Intent intent = new Intent(getActivity(), MainActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });
        ll_shou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shifou.equals("false")) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), MineCollectActivity.class);
                    startActivity(intent);
                }
            }
        });
        ll_wenzhang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shifou.equals("false")) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), MineArticlesActivity.class);
                    startActivity(intent);
                }
            }
        });
        rel_company.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (shifou.equals("false")) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), MineCompanyActivity.class);
                    startActivity(intent);
                }
            }
        });
    }

    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.mine_btn_take_photo:
                    Intent intent = new Intent();
                    intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(intent, 1);
                    touXiangPop.dismiss();
                    break;
                case R.id.mine_btn_pick_photo:
                    Intent intent2 = new Intent();
                    intent2.setAction(Intent.ACTION_GET_CONTENT);
                    intent2.setType("image/*");
                    startActivityForResult(intent2, 2);
                    touXiangPop.dismiss();
                    break;
            }
        }
    };

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            Bitmap bmp = (Bitmap) data.getExtras().get("data");
            civ.setImageBitmap(bmp);
        } else if (requestCode == 2) {
            Uri uri = data.getData();
            civ.setImageURI(uri);
        } else {
            Toast.makeText(getActivity(), "放弃操作", Toast.LENGTH_SHORT).show();
        }
    }
}
